<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Login extends MY_Controller {
     public function __construct() {
        parent::__construct();
        
     }
     
     public function index(){
         $data = array('noShowTabs'=>true, 'showLogOff' => false,'imageBackground' => false,'bootsrapCSS'=>false);
        if(!empty($_POST)){
            if($this->input->post('password') == PASSWORD && $this->input->post('user') == USER){
                self::login();
                redirect('/profesores');
            }else{
                 $this->load->model('usuariomodel');
                $profesor = $this->usuariomodel->getByUsuario($this->input->post('user'));
                if($profesor != null && $profesor['contrasena'] == sha1($this->input->post('password'))){
                    self::login($profesor);
                    redirect('/profesores/update/'.$profesor['id']);
                }else{
                    $data['login_error'] = 'Usuario o contraseña incorrecto';
                }
            }
        }        
        $this->load->view('include/header',$data);
        $this->load->view('login', $data);
        $this->load->view('include/footer');
     }
     
     
     
     public static function login($user = null){
         $_SESSION['usuario']  = true;
         $_SESSION['userData'] = $user;
     }
     
     public function toPedido($hash,$pedido){
          if(sha1(USER.PASSWORD.$pedido) == $hash){
               self::login();
               redirect('/clases/pendiente/'.$pedido);
          }else{             
              redirect('/login');
          }
     }
     
     public function logout(){
         unset($_SESSION['usuario']);
         unset($_SESSION['userData']);
         redirect('/login');
     }
}

